import 'package:flutter/material.dart';

import 'package:xunlongchi/util/app_util.dart';
import 'package:xunlongchi/util/data_util.dart';
import 'package:xunlongchi/widgets/animated_scale.dart';

///器官
class Organ extends StatefulWidget {
  const Organ({Key key}) : super(key: key);

  @override
  _OrganState createState() => _OrganState();
}

class _OrganState extends State<Organ> {
  bool _ready = false;

  @override
  void initState() {
    super.initState();

    () async {
      await Future.delayed(const Duration(milliseconds: 800), () {
        setState(() {
          _ready = true;
        });
      });
    }();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedOpacity(
      opacity: _ready ? 1 : 0,
      duration: const Duration(milliseconds: 500),
      child: AnimatedScale(
        scale: _ready ? 1 : 1.3,
        duration: const Duration(milliseconds: 500),
        child: const OrganBody(),
      ),
    );
  }
}

class OrganBody extends StatelessWidget {
  const OrganBody({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final List<Widget> _item = Iterable<int>.generate(6)
        .map((i) => RotationTransition(
              turns: AlwaysStoppedAnimation(-30 * i / 360 - 90 / 360),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  RotatedBox(
                    quarterTurns: -1,
                    child: Text('${DataUtil.organ[i + 6]}'),
                  ),
                  RotatedBox(
                    quarterTurns: 1,
                    child: Text('${DataUtil.organ[i]}'),
                  ),
                ],
              ),
            ))
        .toList();

    final double _organSize = 640.px;

    return DefaultTextStyle(
      style: TextStyle(
        color: Colors.white,
        fontSize: 36.sp,
      ),
      child: SizedBox(
        width: _organSize,
        height: _organSize,
        child: Stack(
          alignment: Alignment.center,
          children: _item,
        ),
      ),
    );
  }
}
